package com.cyan.app.workflow.activity.SFC;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cyan.app.workflow.R;
import com.cyan.app.workflow.base.activity.BaseActivity;
import com.cyan.app.workflow.bean.MannoBean;
import com.cyan.app.workflow.bean.MannoLabBean;
import com.cyan.app.workflow.customview.PullDownView;
import com.cyan.app.workflow.customview.PullDownView.OnPullDownListener;
import com.cyan.app.workflow.util.AppUtils;

import java.util.List;

/**
 * Created by DXW on 2015/11/24.
 */
public class GdLzActivitiy extends BaseActivity implements OnPullDownListener, AdapterView.OnItemClickListener {
    private TextView mTextView;
    private Button mButton;
    private RelativeLayout mRelativeLayout;
    private MannoLzAdapter adapter;
    private ListView mannoinfo_lv;
    private PullDownView mPullDownView;
    private List<MannoBean> mMannoBeans;
    private Button mButtonDelete;
    private Button mButtonLz;
    private int selectedCount =0;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //super.onActivityResult(requestCode, resultCode, data);
        //这里要判断requestCode和resultCode的值，再做进一步处理
        if(requestCode == 0x0001 && resultCode == RESULT_OK) {
            //initData();
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sfc_gdlz);
        initData();
        initView();

        mButtonDelete = (Button)findViewById(R.id.kjinfo_btn_todelete);
        mButtonDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(selectedCount == 0){
                    AppUtils.showTip(GdLzActivitiy.this, "选择你要操作的记录");
                }else {
                    for (int i = mMannoBeans.size() - 1; i >= 0; i--) {
                        if (mMannoBeans.get(i).isSelected()) {
                            mMannoBeans.remove(i);
                        }
                    }
                    adapter.notifyDataSetChanged();
                }
            }
        });
        mButtonLz = (Button)findViewById(R.id.kjinfo_btn_toliuzhuan);
        mButtonLz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mMannoBeans==null||mMannoBeans.size()==0){
                    AppUtils.showTip(GdLzActivitiy.this, "沒有可流转的工单");
                }
                else {
                    Intent intent = new Intent();
                    intent.setClass(GdLzActivitiy.this, GdlzLzActivity.class);
                    startActivity(intent);
                    //startActivityForResult(intent,0x0001);
                }
            }
        });
    }

    private void initData() {
        mMannoBeans = MannoLabBean.get(this).getMannos();
    }

    @Override
    protected void onResume() {
        super.onResume();
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    private void initView(){
        titleBar();
        /*
		 * 1.使用PullDownView 2.设置OnPullDownListener 3.从mPullDownView里面获取ListView
		 */
        mPullDownView = (PullDownView) findViewById(R.id.kjinfo_lv);

        mPullDownView.setOnPullDownListener(this);

        mannoinfo_lv = mPullDownView.getListView();

        mannoinfo_lv.setOnItemClickListener(this);

        adapter = new MannoLzAdapter(GdLzActivitiy.this);
        mannoinfo_lv.setAdapter(adapter);

        // 隐藏并且禁用头部刷新
        mPullDownView.setHideHeader();
        // 隐藏 并禁用尾部
        mPullDownView.setHideFooter();
    }

    private void titleBar() {
        mTextView =(TextView)findViewById(R.id.top_title_info);
        mTextView.setText("工单流转");
        Button back = (Button) findViewById(R.id.top_title_back);
        back.getBackground().setAlpha(255);// 透明度0~255透明度值 ，值越小越透明
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //返回按钮
                finish();
            }
        });
        mButton = (Button)findViewById(R.id.top_title_refres_with_icon);
        mButton.setVisibility(View.VISIBLE);
        mButton.setText("添加");
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(GdLzActivitiy.this, GdlzNewActivity.class);
                //startActivity(intent);
                startActivityForResult(intent,0x0001);
            }
        });

        mRelativeLayout =(RelativeLayout) findViewById(R.id.kjinfo_rl_foottoliuzhuan);
        mRelativeLayout.setVisibility(View.VISIBLE);
    }

    /**
     * listview单项点击事件
     */
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // Toast.LENGTH_SHORT).show();
    }

    /**
     * listview刷新
     */
    public void onRefresh() {

    }

    /**
     * listview更多
     */
    public void onMore() {
      return;
    }

    public class MannoLzAdapter extends BaseAdapter {
        private LayoutInflater inflater = null;
        public MannoLzAdapter(Context context) {
            inflater = LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mMannoBeans.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            ViewHolder holder = null;
            holder = new ViewHolder();
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.gravity = Gravity.CENTER_VERTICAL;
            convertView = inflater.inflate(R.layout.sfc_gdlz_list, null);
            holder.sfc_mannolz_list_manno = (TextView) convertView.findViewById(R.id.sfc_mannolz_list_manno);
            holder.sfc_mannolz_list_manqty = (TextView) convertView.findViewById(R.id.sfc_mannolz_list_manqty);
            holder.sfc_mannolz_list_area = (TextView) convertView.findViewById(R.id.sfc_mannolz_list_area);
            holder.sfc_mannolz_list_selected = (CheckBox) convertView.findViewById(R.id.sfc_mannolz_list_selected);
            holder.sfc_mannolz_list_manno.setText(mMannoBeans.get(position).getManno());
            holder.sfc_mannolz_list_manqty.setText(String.valueOf(mMannoBeans.get(position).getManqty()));
            holder.sfc_mannolz_list_area.setText(mMannoBeans.get(position).getNextProssname());
            holder.sfc_mannolz_list_selected.setChecked(mMannoBeans.get(position).isSelected());

            holder.sfc_mannolz_list_selected.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    mMannoBeans.get(position).setIsSelected(isChecked);
                    if(isChecked){
                        selectedCount ++;
                    }else {
                        selectedCount --;
                    }
                }
            });

            convertView.setTag(holder);

            return convertView;
        }
        class ViewHolder {
            TextView sfc_mannolz_list_manno;
            TextView sfc_mannolz_list_manqty;
            TextView sfc_mannolz_list_area;
            CheckBox sfc_mannolz_list_selected;
        }
    }

}
